<template>
  <div class="rightbox" v-cloak>
    <h1>公司用户修改</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" label-width="130px">
        <el-form-item
          prop="name"
          label="公司用户名称"
          :rules="[
            { required: true, message: '请填写用户名称', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          prop="phone"
          label="账号(手机号码)"
          :rules="[
            { required: true, message: '请填写用户账号', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.phone"></el-input>
          <span>密码默认为666666</span>
        </el-form-item>
        <el-form-item
          label="用户状态"
          prop="state"
        >
          <el-radio v-model="form.state" label="1">启用</el-radio>
          <el-radio v-model="form.state" label="-1">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
          <el-button  @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        name: '',
        phone: '',
        state: ''
      },
      companyuserindex: ''
    }
  },
  methods: {
    getParams () {
      var that = this
      that.companyuserindex = that.$route.params.companyuserindex
      if (that.companyuserindex) {
        that.$axios.request('post', '/CompanyUser/Eidt', {
          companyuserindex: that.companyuserindex
        })
          .then((res) => {
            if (res.code === 0) {
              that.form = res.data
              that.form.state = res.data.state + ''
            }
          })
      } else {
        that.$router.push({
          name: 'companyuserList'
        })
      }
    },
    submit: function (form) {
      var that = this
      that.$pub.open()
      that.$refs[form].validate((valid) => {
        if (valid) {
          that.$axios.request('post', '/CompanyUser/EidtPost', {
            companyuserindex: that.companyuserindex,
            name: that.form.name,
            phone: that.form.phone,
            state: that.form.state
          })
            .then((res) => {
              if (res.code === 0) {
                that.$axios.alert('修改成功', '提示', function () {
                  that.$router.push({
                    name: 'companyuserList'
                  })
                })
              }
            })
        } else {
          return false
        }
      })
    },
    resetForm: function (form) {
      this.$refs[form].resetFields()
    }
  },
  mounted () {
    this.getParams()
  }
}
</script>
